/**********************************************
* SFW_TcpPort.cpp
***********************************************/
#include "framework.h"

/**********************************************
* constructor
**********************************************/
SFW_ClientAndRemoteInfo::SFW_ClientAndRemoteInfo()
	:clientIP(NULL),
	remoteIP(NULL)
{}

SFW_ClientAndRemoteInfo::~SFW_ClientAndRemoteInfo()
{
	FREE_BUFFER(clientIP);
	FREE_BUFFER(remoteIP);
}
	
/**********************************************
* member method
* setIPAddr 
**********************************************/
void SFW_ClientAndRemoteInfo::setIPAddr(char* strAddr, enCntType type)
{
	switch(type)
	{
	case client:
		clientIP = strAddr;
		break;
	case remote:
		remoteIP = strAddr;
		break;
	default:
		break;
	}
}

/**********************************************
* member method
* setIPAddr(overload) 
**********************************************/
void SFW_ClientAndRemoteInfo::setIPAddr(int af, const void* numAddr, enCntType type)
{
	switch(type)
	{
	case client:
		clientIP = Inet_ntop(af, numAddr);
		break;
	case remote:
		remoteIP = Inet_ntop(af, numAddr);
		break;
	default:
		break;
	}
}

/**********************************************
* member method
* setPort 
**********************************************/
void SFW_ClientAndRemoteInfo::setPort(uint16_t port, enCntType type)
{
	switch(type)
	{
	case client:
		clientPort = port;
		break;
	case remote:
		remotePort = port;
		break;
	default:
		break;
	}
}

/**********************************************
* member method
* setIPAndPort 
**********************************************/
void SFW_ClientAndRemoteInfo::setIPAndPort(struct sockaddr_in* cntInfo, enCntType type)
{
	int af = cntInfo->sin_family;
	char* numIp = (char*)&cntInfo->sin_addr.s_addr;
	short port = ntohs(cntInfo->sin_port);

	setPort(port, type);
	setIPAddr(af, numIp, type);
}

/**********************************************
* member method
* Inet_ntop 
**********************************************/
char* SFW_ClientAndRemoteInfo::Inet_ntop(int af, const void* src)
{
	char* dst;
	socklen_t len;
	if(af == AF_INET)
	{
		/* IPv4 */
		len = INET_ADDRSTRLEN;
	}
	
	else if(af == AF_INET6)
	{
		/* IPv6 */
		len = INET6_ADDRSTRLEN;
	}
	
	else
	{
		return NULL;
	}
	
	dst = (char*)malloc(len + 1);
	memset(dst, 0x00, len + 1);
	
	char* buf = (char*)::inet_ntop(af, src, dst, len);
	
	return buf;
}

/**********************************************
* member method
* Displayinfo 
**********************************************/
void SFW_ClientAndRemoteInfo::DisplayInfo(char* msg)
{
	/* (C)(IP :port) <<=msg=>> (R)(IP :port)*/
	LOG_INFO("(C)(%s :%d) <<=%s=>> (R)(%s :%d)", clientIP, clientPort, msg, remoteIP, remotePort);
}

